package utilities;

import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class ChunkModif extends PdfPageEventHelper {

	@Override
	public void onGenericTag(PdfWriter writer, Document pdfDocument,
			Rectangle rect, String text) {
		if ("strip".equals(text))
			strip(writer.getDirectContent(), rect);
		else if ("ellipse".equals(text))
			ellipse(writer.getDirectContentUnder(), rect);
		else if ("roundRect".equals(text)){
			roundRect(writer.getDirectContentUnder(), rect);
		}else if ("leftRR".equals(text)){
			leftRR(writer.getDirectContentUnder(), rect);
		}else if ("rightRR".equals(text)){			
			rightRR(writer.getDirectContentUnder(), rect);
		}else {
			rect(writer.getDirectContentUnder(), rect);			
		}
	}

	public void strip(PdfContentByte content, Rectangle rect) {
		content.rectangle(rect.getLeft() - 1, rect.getBottom() - 5f,
				rect.getWidth(), rect.getHeight() + 8);
		content.rectangle(rect.getLeft(), rect.getBottom() - 2,
				rect.getWidth() - 2, rect.getHeight() + 2);
		float y1 = rect.getTop() + 0.5f;
		float y2 = rect.getBottom() - 4;
		for (float f = rect.getLeft(); f < rect.getRight() - 4; f += 5) {
			content.rectangle(f, y1, 4f, 1.5f);
			content.rectangle(f, y2, 4f, 1.5f);
		}
		content.eoFill();
	}

	public void ellipse(PdfContentByte content, Rectangle rect) {
		content.saveState();
		content.setRGBColorFill(0x00, 0x00, 0xFF);
		content.ellipse(rect.getLeft() - 3f, rect.getBottom() - 5f,
				rect.getRight() + 3f, rect.getTop() + 3f);
		content.fill();
		content.restoreState();
	}

	public void roundRect(PdfContentByte content, Rectangle rect) {
		content.saveState();
		content.setRGBColorFill(0x00, 0xFF, 0x00);
		content.roundRectangle(rect.getLeft()-0.5f, rect.getBottom()-1.5f,
				rect.getWidth()+3f , rect.getHeight(), 3);
		
		content.fill();
		content.restoreState();
	}
	
	public void roundRectStroke(PdfContentByte content, Rectangle rect) {
		content.saveState();
		content.setRGBColorFill(0xFF, 0xFF, 0xFF);
		content.setRGBColorStroke(0x00, 0xFF, 0x00);
		content.roundRectangle(rect.getLeft()-0.5f, rect.getBottom()-1.5f,
				rect.getWidth()+3f , rect.getHeight(), 3);				
		content.fillStroke();
		content.restoreState();
	}
	
	public void leftRR(PdfContentByte content, Rectangle rect) {
		content.saveState();
		content.setRGBColorFill(0x00, 0x00, 0xFF);
		content.roundRectangle(rect.getLeft()-3f, rect.getBottom()-1.5f,
				6 , rect.getHeight(), 3);	
		content.rectangle(rect.getLeft(), rect.getBottom()-1.5f,
				rect.getWidth() , rect.getHeight());
		
		content.fill();
		content.restoreState();
	}
	
	public void rightRR(PdfContentByte content, Rectangle rect) {
		content.saveState();
		content.setRGBColorFill(0x00, 0x00, 0xFF);
		content.roundRectangle(rect.getLeft()+ rect.getWidth()-1f, rect.getBottom()-1.5f,
				6 , rect.getHeight(), 3);
		content.rectangle(rect.getLeft()-0.5f, rect.getBottom()-1.5f,
				rect.getWidth()+3 , rect.getHeight());		
		content.fill();
		content.restoreState();
	}
	
	public void rect(PdfContentByte content, Rectangle rect) {
		content.saveState();
		content.setRGBColorFill(0xFF, 0xFF, 0xFF);
		content.rectangle(rect.getLeft()-0.5f, rect.getBottom()-1.5f,
				rect.getWidth()+3 , rect.getHeight());		
		content.fill();
		content.restoreState();
	}
		
}
